<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="applicantName">
        <el-input v-model="queryParams.applicantName" placeholder="请输入申请人姓名" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="申请小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择小区" filterable clearable size="mini">
          <el-option v-for="estate in estatesOptions"
                     :key="estate.id"
                     :label="estate.estateName"
                     :value="estate.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="approverName">
        <el-input v-model="queryParams.approverName" placeholder="请输入审核人姓名" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="提报时间">
        <el-date-picker v-model="daterangeCreateTime" size="mini" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item label="审核时间">
        <el-date-picker v-model="daterangeApproverTime" size="mini" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['module:templateApprover:edit']">审批
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"-->
<!--                   v-hasPermi="['module:templateApprover:remove']">删除-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="approverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="${comment}" align="center" prop="id" v-if="false"/>
      <el-table-column label="审批单号" align="center" prop="approverNo" width="120px"/>
      <el-table-column label="申请小区" align="center" prop="estateName" width="100px"/>
      <el-table-column label="申请内容" align="center" prop="inOrOut"  width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.inOrOut === 0" style="cursor: pointer" :type="'warning'">取消折扣</el-tag>
          <el-tag v-if="scope.row.inOrOut === 1" style="cursor: pointer" :type="''">申请折扣</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请月数" align="center" prop="applicantMonth" width="100px">
        <template slot-scope="scope">
          {{scope.row.applicantMonth+"个月"}}
        </template>
      </el-table-column>
      <el-table-column label="单月动账金额" align="center" prop="totalPrice"width="120px">
        <template slot-scope="scope">
          {{scope.row.totalPrice +"元"}}
        </template>
      </el-table-column>
      <el-table-column label="动账总金额" align="center" prop="totalPrice" width="100px">
        <template slot-scope="scope">
          {{(scope.row.totalPrice * scope.row.applicantMonth).toFixed(2)+"元"}}
        </template>
      </el-table-column>
      <el-table-column label="申请人姓名" align="center" prop="applicantName" width="120px"/>
      <el-table-column label="申请时间" align="center" prop="createTime" width="180px"/>
      <el-table-column label="审批状态" align="center" prop="state" width="100px">
        <template slot-scope="scope">
          <el-popover v-if="scope.row.state===0" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批意见：{{scope.row.approverRemark}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="'warning'" slot="reference">未审核</el-tag>
          </el-popover>
          <el-popover v-if="scope.row.state===1" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批意见：{{scope.row.approverRemark}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="'success'" slot="reference">已通过</el-tag>
          </el-popover>
          <el-popover v-if="scope.row.state===2" placement="right" width="300" trigger="hover">
            <el-row v-loading="loadingState" >
              <el-col>
                <el-row>
                  审批意见：{{scope.row.approverRemark}}
                </el-row>
              </el-col>
            </el-row>
            <el-tag style="cursor: pointer" :type="'danger'" slot="reference">已驳回</el-tag>
          </el-popover>
<!--          <div>-->
<!--            <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'warning'">未审核</el-tag>-->
<!--            <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="'success'">已通过</el-tag>-->
<!--            <el-tag v-if="scope.row.state === 2" style="cursor: pointer" :type="'danger'">已驳回</el-tag>-->
<!--          </div>-->
        </template>
      </el-table-column>
      <el-table-column label="审核人姓名" align="center" prop="approverName" width="120px"/>
      <el-table-column label="审核时间" align="center" prop="approverTime" width="180px"/>

      <el-table-column label="备注信息" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-if="scope.row.state ==0"
                     v-hasPermi="['module:templateApprover:edit']">审批
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-if="scope.row.state ==0"
                     v-hasPermi="['module:templateApprover:remove']">取消审批单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改物业费相关费用模板的修改审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-table v-loading="loading" :data="templateList">
          <el-table-column label="小区" align="center" prop="estateName" width="100px"/>
          <el-table-column label="户号" align="center" prop="roomNo" width="100px"/>
          <el-table-column label="申请年份" align="center" prop="yearNum" width="100px"/>
          <el-table-column label="申请月份" align="center" prop="monthNum" width="100px"/>
          <el-table-column label="费用原价" align="center" prop="totalPrice" width="100px"/>
          <el-table-column label="费用类型" align="center" prop="feeType"  width="100px">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.feeType === 'rubbish'" style="cursor: pointer" :type="'warning'">垃圾处理费</el-tag>
              <el-tag v-if="scope.row.feeType === 'wyf'" style="cursor: pointer" :type="''">物业费</el-tag>
            </template>
          </el-table-column>

        </el-table>


        <el-form-item label="审批" prop="state">
          <el-radio-group v-model="form.state" size="mini">
            <el-radio :value="1" :label="1">通过</el-radio>
            <el-radio :value="2" :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="审批意见" prop="approverRemark" style="width: 600px">
          <el-input v-model="form.approverRemark" placeholder="请输入审批意见" size="mini" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApprover,
  getApprover,
  delApprover,
  addApprover,
  updateApprover,
  exportApprover
} from "@/api/module/property/approver";
import {
  approverListTemplate
} from "@/api/module/estate/wyfTemplate";
import { listAllEstate } from "@/api/module/estate/estate";
export default {
  name: "Approver",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物业费相关费用模板的修改审核表格数据
      approverList: [],
      //费用明细的表格
      templateList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 提报时间时间范围
      daterangeCreateTime: [],
      // 审核时间时间范围
      daterangeApproverTime: [],
      estatesOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateId:undefined,
        applicantName:undefined,
        approverName:undefined,
        beginCreateTime:undefined,
        endCreateTime:undefined,
        beginApproverTime:undefined,
        endApproverTime:undefined,
      },
      query: {
        approverId:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getEstateList();
    this.getList();
  },
  methods: {
    /** 查询物业费相关费用模板的修改审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.beginCreateTime = this.daterangeCreateTime[0];
        this.queryParams.endCreateTime = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeApproverTime && '' != this.daterangeApproverTime) {
        this.queryParams.beginApproverTime= this.daterangeApproverTime[0];
        this.queryParams.endApproverTime= this.daterangeApproverTime[1];
      }
      listApprover(this.queryParams).then(response => {
        this.approverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getEstateList() {
      listAllEstate().then(response => {
        this.estatesOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        templateId: undefined,
        inOrOut: undefined,
        totalPrice: undefined,
        state: undefined,
        applicantName: undefined,
        applicantId: undefined,
        approverName: undefined,
        approverId: undefined,
        createTime: undefined,
        approverTime: undefined,
        remark: undefined,
        applicantMonth:undefined,
        approverRemark:undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeApproverTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物业费相关费用模板的修改审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      this.query.approverId = id;
      approverListTemplate(this.query).then(response => {
        this.templateList = response;
      });
      getApprover(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改物业费相关费用模板的修改审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateApprover(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除物业费相关费用模板的修改审核编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delApprover(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有物业费相关费用模板的修改审核数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportApprover(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
